/**
 * All rights Reserved, Designed By OprCalf
 * Copyright:    Copyright(C) 2016-2020
 * Company       LengYin Ltd.
 */

package com.platform.gateway.common.request;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;

import lombok.extern.slf4j.Slf4j;

/**
 * @projectName:  platform-gateway
 * @package:      com.platform.gateway.common.request
 * @className:    Request.java
 * @description:  请求设置
 * @author:       OprCalf
 * @date:         2020年3月27日
 */
@Service
@Slf4j
public class Request {

    @Autowired
    private HttpServletRequest request;

    /**
     * 重写FastJson的值过滤,当为空值的时候返回"",不然转字符串会直接把属性过滤掉
     * @author OprCalf
     */
    public static ValueFilter filter = (obj, s, v) -> {
        if (v == null) {
            return "";
        }
        return v;
    };

    /**
     * 初始化web的请求头信息
     * @author OprCalf
     * @param needUser
     * @return HttpHeaders
     */
    public HttpHeaders initWebHeader() {
        try {
            final String access_token = request.getHeader("access_token");
            final HttpHeaders headers = new HttpHeaders();
            headers.add("access_token", access_token);
            return headers;
        }
        catch (final Exception e) {
            log.error("{}", e.fillInStackTrace());
            return new HttpHeaders();
        }
    }

    /**
     * 返回错误信息
     * @author OprCalf
     * @param result
     * @return JSONObject
     */
    public static JSONObject returnError(Object result) {
        final JSONObject json = new JSONObject();
        final JSONObject data = new JSONObject();
        json.put("respStatus", "01");
        json.put("respDesc", result);
        json.put("data", data);
        return json;
    }

    /**
     * 返回正确的信息
     * @author OprCalf
     * @param successMsg
     * @param data
     * @return String
     */
    public static JSONObject returnSuccess(String successMsg, Object data) {
        final JSONObject json = new JSONObject();
        json.put("respStatus", "00");
        json.put("respDesc", successMsg);
        json.put("data", data);
        return json;
    }

}
